home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1002 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: bill@gibbons.org (Bill Gibbons)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: sample auto_ptr template
  5. Date: 08 Apr 1996 10:16:05 PDT
  6. Organization: -none-
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <bill-0804960932250001@bgibbons.vip.best.com>
  9. References: <009A04DA6A831C40.49800EAC@ittpub.nl> <4k0m72$gm1@jabba.lehman.com> <bill-0504961003150001@bgibbons.vip.best.com> <4k4noe$igl@jabba.lehman.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Mon, 08 Apr 1996 09:32:25 -0700
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMWlJ1ky4NqrwXLNJAQEHjgH/czVIK5XtZNJ9zkSpRevuxQcJcFWdDfzo
  14.     Ri4BbF6sB3H06GY9+bHY3lKMG05a9pW1s39DPT2Pzz7uyF8oMZ/pgw==
  15.     =j2Xs
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. In article <4k4noe$igl@jabba.lehman.com>, ajay@lehman.com (Ajay Kamdar) wrote:
  19.  
  20. > ...
  21. > It is not clear at all that the copy semantics of auto_ptr
  22. > are essential for exception-safe transfer of resources.
  23. > The same example coded as follows does not use
  24. > the copy semantics of auto_ptr:
  25. >     extern X* get_X();   // returns a resource acquired
  26. >                          // by the callee, to be deleted
  27. >                          // by the caller.
  28. >     void f() {
  29. >         auto_ptr<X> ptr = get_X();
  30. >                         // resource allocated by get_X()
  31. >         ...
  32. >    }
  33. > And get_X() is not unnecessarily complicated either:
  34. >    X* get_X()
  35. >    {
  36. >       auto_ptr<X> p = new X;
  37. >       // ... stuff that could throw an exception
  38. >       // We got here. Means normal return.
  39. >       return p.release();
  40. >    }
  41. > What's wrong with this? It doesn't require copy semantics
  42. > for auto_ptr. Yet both the caller and the callee
  43. > are exception safe and there is no loss of clarity.
  44.  
  45. The problem is that it requires handling the raw pointer.
  46. Any time you transfer ownership by using release() to extract
  47. the raw pointer and then later use the raw pointer to
  48. construct another auto_ptr, there is a window where there is
  49. no exception safety.
  50.  
  51. Of course you can carefully craft the code to make sure that
  52. no exceptions can be propagated during the window.  But there
  53. are two problems:
  54.  
  55.   (1) The maintainers of the code may not be as careful about
  56.       exception safety.  When everything is handled by
  57.       auto_ptr the risk of bugs creeping in is smaller.  Such
  58.       bugs are very difficult to find by testing.
  59.  
  60.   (2) The interface of get_X does not implicitly document
  61.       that the returned pointer refers to an object which
  62.       should be automatically deleted on an exception.
  63.  
  64. -- 
  65. Bill Gibbons
  66. bill@gibbons.org
  67. ---
  68. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  69.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  70.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  71.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  72.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  73. ]
  74.